package com.linguochao.rabbitmq.config;

import org.springframework.amqp.core.*;

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author linguochao
 * @Date 2020/7/15 17:49
 */
@Configuration
public class RabbitMQConfig {

    /**
     * mq的容器工厂参数，增加并发处理数量即可实现多线程处理监听队列
     *
     * @param configurer
     * @param connectionFactory
     * @return
     */
    @Bean("customContainerFactory")
    public SimpleRabbitListenerContainerFactory containerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConcurrentConsumers(1);  //设置线程数
        factory.setMaxConcurrentConsumers(3); //最大线程数
        configurer.configure(factory, connectionFactory);
        return factory;
    }


    //======================直接模式========================================
    //队列名称
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";

    //声明QUEUE_INFORM_SMS队列
    @Bean(QUEUE_INFORM_SMS)
    public Queue QUEUE_INFORM_SMS() {
        return new Queue(QUEUE_INFORM_SMS);
    }
    //======================直接模式========================================


    //======================分列模式========================================
    //队列名称
    public static final String QUEUE_DATA_MYSQL = "queue_data_mysql";
    public static final String QUEUE_DATA_SOLR = "queue_data_solr";
    public static final String EXCHANGE_DATA = "exchange_data";

    //声明交换机
    @Bean(EXCHANGE_DATA)
    public Exchange EXCHANGE_DATA() {
        //durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.topicExchange(EXCHANGE_DATA).durable(true).build();
    }

    //声明QUEUE_DATA_MYSQL队列
    @Bean(QUEUE_DATA_MYSQL)
    public Queue QUEUE_DATA_MYSQL() {
        return new Queue(QUEUE_DATA_MYSQL);
    }

    //声明QUEUE_DATA_SOLR队列
    @Bean(QUEUE_DATA_SOLR)
    public Queue QUEUE_DATA_SOLR() {
        return new Queue(QUEUE_DATA_SOLR);
    }

    //QUEUE_DATA_MYSQL队列绑定交换机
    @Bean
    public Binding BINDING_DATA_MYSQL(@Qualifier(QUEUE_DATA_MYSQL) Queue queue,
                                      @Qualifier(EXCHANGE_DATA) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    //QUEUE_DATA_SOLR队列绑定交换机
    @Bean
    public Binding BINDING_DATA_SOLR(@Qualifier(QUEUE_DATA_SOLR) Queue queue,
                                     @Qualifier(EXCHANGE_DATA) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
    //======================分列模式========================================


    //======================主题模式========================================
    //队列名称
    public static final String QUEUE_QQ = "mq.topic.qq.queue";
    public static final String QUEUE_WX = "mq.topic.wx.queue";
    public static final String QUEUE_LOG = "mq.topic.log.queue";
    public static final String EXCHANGE_TOPIC = "exchange_data";

    //声明交换机
    @Bean(EXCHANGE_TOPIC)
    public Exchange EXCHANGE_TOPIC() {
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPIC).durable(true).build();
    }

    //声明QUEUE_QQ队列
    @Bean(QUEUE_QQ)
    public Queue QUEUE_QQ() {
        return new Queue(QUEUE_QQ);
    }

    //声明QUEUE_WX队列
    @Bean(QUEUE_WX)
    public Queue QUEUE_WX() {
        return new Queue(QUEUE_WX);
    }

    //声明QUEUE_LOG队列
    @Bean(QUEUE_LOG)
    public Queue QUEUE_LOG() {
        return new Queue(QUEUE_LOG);
    }

    //队列绑定交换机
    @Bean
    public Binding BINDING_TOPIC_QQ(@Qualifier(QUEUE_QQ) Queue queue,
                                    @Qualifier(EXCHANGE_TOPIC) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("qq.*").noargs();
    }

    //队列绑定交换机
    @Bean
    public Binding BINDING_TOPIC_WX(@Qualifier(QUEUE_WX) Queue queue,
                                    @Qualifier(EXCHANGE_TOPIC) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("wx.*").noargs();
    }

    //队列绑定交换机
    @Bean
    public Binding BINDING_TOPIC_LOG(@Qualifier(QUEUE_LOG) Queue queue,
                                     @Qualifier(EXCHANGE_TOPIC) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("*.log").noargs();
    }
    //======================主题模式========================================

}
